package com.it.core.web.controller;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.it.core.po.User;
import com.it.core.service.UserService;

//用户控制类
@Controller
public class UserController {
	//依赖注入
	@Autowired
	private UserService userService;

	//用户登陆
	@RequestMapping(value="/login.action", method = RequestMethod.GET)
	public String login(String username, String password, Model model, HttpSession session) {
		User user = userService.findUser(username, password);
		if (user != null) {
			// 将用户对象添加到Session
			session.setAttribute("USER_SESSION", user);
			//跳转到主页面
			return "redirect:list.action";
		}
		model.addAttribute("msg", "账户或密码错误，请重新输入！");
		// 返回到登陆界面
		return "login";
		
	}
	
	@RequestMapping(value="/tomain",method = RequestMethod.GET)
	public String tomain() {
		return "main";
	}
	
	@RequestMapping(value="/tozhuce",method = RequestMethod.GET)
	public String tozhuce() {
		return "zhuce";
	}
	
	@RequestMapping(value="/tologin",method = RequestMethod.GET)
	public String tologin() {
		return "login";
	}
	
	// 添加用户
	@RequestMapping(value="/addUser.action", method = RequestMethod.GET)
	public String addUser(User user,Model model) {
		
		int rows = userService.addUser(user);
		if(rows > 0) {
			model.addAttribute("mag", "注册成功，可以登陆了");
			return "login";
		}
		model.addAttribute("msg", "注册失败");
		return "zhuce";
		
	}
}
